package com.internetCafes.spms.web.job.task.customer;

import cn.hutool.core.date.DateUtil;
import com.internetCafes.spms.web.customer.common.cont.CustomerConst;
import com.internetCafes.spms.web.customer.entity.customer.CustomerFlowCount;
import com.internetCafes.spms.web.customer.model.customer.flow.CustomerFlowDo;
import com.internetCafes.spms.web.customer.model.customer.flow.CustomerFlowPo;
import com.internetCafes.spms.web.customer.service.customer.ICustomerFlowCountService;
import com.internetCafes.spms.web.customer.service.customer.ICustomerFlowService;
import com.internetCafes.spms.web.customer.service.customer.ICustomerService;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component("customerTask")
public class CustomerTask {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ICustomerFlowService flowService;
    @Autowired
    private ICustomerFlowCountService flowCountService;

    /**
     * 客户操作统计 = 周
     */
    public void customerFlowCount() {
// 获取上周流转记录
        List<CustomerFlowPo> flowList = flowService.poList(new CustomerFlowDo().setActionTag(CustomerConst.FlowPoListActionTag.WEEK_COUNT.getId()));
        Date now = new Date();
        if (CollectionUtil.isEmpty(flowList)) {
            return;
        }
        List<CustomerFlowCount> flowCountDoList = new ArrayList<>();
        CustomerFlowCount customerFlowCount;
        Map<Long, Map<Integer, List<CustomerFlowPo>>> flowListGroupByActionTagAndTenantId
                = flowList.stream().collect(Collectors.groupingBy(CustomerFlowPo::getTenantId, Collectors.groupingBy(CustomerFlowPo::getActionTag)));

        Long tenantId;
        Map<Integer, List<CustomerFlowPo>> flowListGroupByActionTag;
        for (Map.Entry<Long, Map<Integer, List<CustomerFlowPo>>> flowListGroupByActionTagEntry : flowListGroupByActionTagAndTenantId.entrySet() ) {
            tenantId = flowListGroupByActionTagEntry.getKey();
            flowListGroupByActionTag = flowListGroupByActionTagEntry.getValue();
            customerFlowCount = new CustomerFlowCount(1L, now, tenantId);
            if (flowListGroupByActionTag.containsKey(CustomerConst.FlowActionTag.CREATE.getId())) {
                customerFlowCount.setCreateAmount(flowListGroupByActionTag.get(CustomerConst.FlowActionTag.CREATE.getId()).size());
            }
            if (flowListGroupByActionTag.containsKey(CustomerConst.FlowActionTag.FLOW_USER_CHANGE.getId())) {
                customerFlowCount.setTransferAmount(flowListGroupByActionTag.get(CustomerConst.FlowActionTag.CREATE.getId()).size());
            }
            if (flowListGroupByActionTag.containsKey(CustomerConst.FlowActionTag.DELETE.getId())) {
                customerFlowCount.setDeleteAmount(flowListGroupByActionTag.get(CustomerConst.FlowActionTag.DELETE.getId()).size());
            }
            if (flowListGroupByActionTag.containsKey(CustomerConst.FlowActionTag.TO_INTENTION.getId())) {
                customerFlowCount.setToInterestAmount(flowListGroupByActionTag.get(CustomerConst.FlowActionTag.TO_INTENTION.getId()).size());
            }
            if (flowListGroupByActionTag.containsKey(CustomerConst.FlowActionTag.TO_DEAL.getId())) {
                customerFlowCount.setToInterestAmount(flowListGroupByActionTag.get(CustomerConst.FlowActionTag.TO_DEAL.getId()).size());
            }
            if (flowListGroupByActionTag.containsKey(CustomerConst.FlowActionTag.INTENTION_MOVE.getId())) {
                customerFlowCount.setRemoveAmount(flowListGroupByActionTag.get(CustomerConst.FlowActionTag.INTENTION_MOVE.getId()).size());
            }
            if (flowListGroupByActionTag.containsKey(CustomerConst.FlowActionTag.DEAL_MOVE.getId())) {
                customerFlowCount.setRemoveAmount( customerFlowCount.getRemoveAmount() + flowListGroupByActionTag.get(CustomerConst.FlowActionTag.DEAL_MOVE.getId()).size());
            }
            flowCountDoList.add(customerFlowCount);
        }
        flowCountService.saveBatch(flowCountDoList);
    }


    /**
     * 更新客户按年统计 是否成交标签
     */
    public void updateYearDealFlag() {
        // 1-无今年成交客户数据 2-今年成交标签如果全部是零-暂时不允许运行该定时器，以防重复执行的时候，直接将成交记录都覆盖掉，该定时器只在一年的开头第一天的前两小时允许被处罚


        customerService.updateYearDealFlag();
    }
}
